<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/static/layui/css/layui.css"/>
    <script src="/static/js/jquery.js"></script>
    <script src="/static/layui/layui.js"></script>
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style>
       .project{
           letter-spacing: 10px;
           font-size: 50px;
           margin-left: 30px;
           margin-top: 20px;
       }
       .layui-form{
           margin-top: 50px;
       }
    </style>
</head>
<body>


<h1 class="project">实例</h1>
<form class="layui-form" action="" id="searchForm">
    <div class="layui-form-item">
        <br>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="phone" id="phone"  class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <a  onclick="doSearch1()" class="layui-btn layui-btn-radius" >搜索</a>
            <button type="reset" class="layui-btn  layui-btn-radius ">重置</button>
        </div>
    </div>
</form>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">创建</button>
        <button class="layui-btn layui-btn-danger" lay-event="delete">删除</button>
    </div>
</script>
<div style="max-width: 728px;">
  <table class="layui-hide" id="test-dropdown-table" lay-filter="test-dropdown-table"></table>
</div>
<script type="text/html" id="test-dropdown-toolbar-barDemo">
  <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="start">启动</a>
  <a class="layui-btn layui-btn-xs" lay-event="more">更多 <i class="layui-icon layui-icon-down"></i></a>
</script>

<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
layui.use(['dropdown', 'util', 'layer', 'table'], function(){
  var dropdown = layui.dropdown
  ,util = layui.util
  ,layer = layui.layer
  ,table = layui.table
  ,$ = layui.jquery;
    // dropdown 在表格中的应用
  table.render({
    elem: '#test-dropdown-table'
    ,url: '/wyh0282/serverJson'/*tpa=https://www.layuiweb.com/test/table/demo5.json*/
    ,title: '用户数据表'
    ,toolbar: '#toolbarDemo'
    ,height:400
    ,width:1250
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
                    /*  ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}*/
                    ,{field:'name', title:'实例名称', width:70}
                    ,{field:'id', title:'标识', width:10}
                    ,{field:'image_name', title:'映像名称', width:100}
                    ,{field:'ip', title:'IP地址', width:140}
                    ,{field:'original_name', title:'大小',width:100}
                    ,{field:'key_name', title:'密钥对',width:100}
                    ,{field:'status', title:'状态', width:70}
                    ,{field:'OS-EXT-AZ:availability_zone', title:'可用域', width:100}
                    ,{field:'task_state', title:'任务', width:100}
                    ,{field:'power_state', title:'电源状态', width:100}
                    ,{field:'created_date', title:'创建以来的时间', width:170}
                    ,{fixed: 'right', title:'操作', toolbar: '#test-dropdown-toolbar-barDemo', width:150}
    ]]
    ,limits: [3]
    ,page: true
  });
  //工具栏事件
        table.on('toolbar(test-dropdown-toolbar)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                case 'add':
                    parent.layer.open({
                        type: 2
                        ,offset:'auto'
                        ,title:'创建映像'
                        ,content:'/wyh0282/imagesAdd'
                        ,area:['1000px','600px']
                        ,btn:'关闭全部'
                        ,btnAlign:'c'
                        ,yes:function () {
                            parent.layer.closeAll();
                        }
                        , end:function () {
                            location.reload(); //踩了好多坑才弄出来的，最后加载当前页面
                        }

                    });
                    break;
            };
        });

  //行工具事件
  table.on('tool(test-dropdown-table)', function(obj){
    var server_id = obj.data.id; //获得当前行数据
    var that = this
    if(obj.event === 'start'){
       $.get("/wyh0282/serverStart",{id:server_id},function (respData) {
           if(respData.code==1){
               layer.msg("启动实例成功！");
               window.parent.location.reload();
           }else {
               layer.msg("启动实例失败！");

           }
       });
    }else if(obj.event === 'more'){
      dropdown.render({
        elem: that
        ,show: true //外部事件触发即显示
        ,data: [{
          title: '创建快照'
          ,id: 'createImage'
        }, {
          title: '控制台'
          ,id: 'console'
        }, {
          title: '重启实例'
          ,id: 'restart'
        },{
           title: '关闭实例'
          ,id: 'serverEnd'
          },{
          title: '暂停实例'
          ,id: 'suspend'
        },{
          title: '取消暂停'
          ,id: 'unsuspend'
        }, {
          title: '挂起实例'
          ,id: 'mount'
        },{
          title: '恢复挂起'
          ,id: 'unmount'
        }, {
          title: '删除'
          ,id: 'del'
        },{
          title: '查看实例'
          ,id: 'showServer'
        },]
        ,click: function(data, othis){
          //根据 id 做出不同操作
          if(data.id === 'del'){
            layer.confirm('真的删除行么', function(index){
              layer.msg("某行id"+ server_id);
              obj.del();
              layer.close(index);
            });
          }else if(data.id === 'console'){
                parent.layer.open({
                    type: 2
                    ,offset:'auto'
                    ,title:'控制台'
                    ,content:'/wyh0282/operationConsole'
                    ,area:['1000px','600px']
                    ,btn:'关闭全部'
                    ,btnAlign:'c'
                    ,yes:function () {
                        parent.layer.closeAll();

                    }
                    ,end:function (){
                        location.reload();
                    }
                });
          } else if(data.id === 'showServer'){
               parent.layer.open({
                                    type: 2
                                    ,offset:'auto'
                                    ,title:'实例详情'
                                    ,content:'/wyh0282/serverMessage?server_id='+server_id
                                    ,area:['1000px','600px']
                                    ,btn:'关闭全部'
                                    ,btnAlign:'c'
                                    ,yes:function () {
                                        parent.layer.closeAll();
                                    }
                                    , end:function () {
                                        location.reload(); //踩了好多坑才弄出来的，最后加载当前页面
                                    }

                                });
          }else if(data.id === 'serverEnd'){
              $.get("/wyh0282/serverEnd",{id:server_id},function (respData) {
                if(respData.code == 1){
                layer.msg("关闭实例成功！");
                window.parent.location.reload();
                }else {
                  layer.msg("关闭实例失败！");
                }});
          }else if(data.id === 'restart'){
              $.get("/wyh0282/reServerStart",{id:server_id},function (respData) {
                if(respData.code == 1){
                layer.msg(respData.msg);
                window.parent.location.reload();
                }else {
                  layer.msg(respData.msg);
                }});
          }else if(data.id === 'suspend'){
              $.get("/wyh0282/serverSuspend",{id:server_id},function (respData) {
                if(respData.code == 1){
                layer.msg(respData.msg);
                window.parent.location.reload();
                }else {
                  layer.msg(respData.msg);
                }});
          }else if(data.id === 'unsuspend'){
              $.get("/wyh0282/noServerSuspend",{id:server_id},function (respData) {
                if(respData.code == 1){
                layer.msg(respData.msg);
                window.parent.location.reload();
                }else {
                  layer.msg(respData.msg);
                }});
          }else if(data.id === 'mount'){
               $.get("/wyh0282/serverHang",{id:server_id},function (respData) {
                if(respData.code == 1){
                layer.msg(respData.msg);
                window.parent.location.reload();
                }else {
                  layer.msg(respData.msg);
                }});
          }else if(data.id === 'showServer'){
              $.get("/wyh0282/unmount",{id:server_id},function (respData) {
                if(respData.code == 1){
                layer.msg(respData.msg);
                window.parent.location.reload();
                }else {
                  layer.msg(respData.msg);
                }});
          }
          else if(data.id === 'showServer'){
                $.get("/wyh0282/serverHang",{id:server_id},function (respData) {
                if(respData.code == 1){
                layer.msg(respData.msg);
                window.parent.location.reload();
                }else {
                  layer.msg(respData.msg);
                }});
          }else if(data.id === 'createImage'){
                parent.layer.open({
                                    type: 2
                                    ,offset:'auto'
                                    ,title:'创建实例快照'
                                    ,content:'/wyh0282/serverSnapshotHtml?server_id='+server_id
                                    ,area:['1000px','600px']
                                    ,btn:'创建快照'
                                    ,btnAlign:'c'
                                    ,yes:function () {
                                        parent.layer.closeAll();
                                    }
                                    , end:function () {
                                        location.reload(); //踩了好多坑才弄出来的，最后加载当前页面
                                    }

                                });
          }else {
            layer.msg('得到表格下拉菜单 id：'+ data.id);
          }
        }
        ,align: 'right' //右对齐弹出（v2.6.8 新增）
        ,style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
      });
    }
  });
});
</script>


</body>
</html>